import { Node } from '@antv/x6'

export class Group extends Node {
  constructor(options) {
    super(options)
    this.collapsed = false
    this.expandSize = null
  }

  // 设置节点配置
  static config() {
    return {
      markup: [
        {
          tagName: 'rect',
          selector: 'body',
        },
        {
          tagName: 'text',
          selector: 'label',
        },
        // 新增左上角描述文本
        {
          tagName: 'text',
          selector: 'desc',
        },
      ],
      attrs: {
        body: {
          fill: '#f0f8ff',
          stroke: '#5F95FF',
          strokeWidth: 2,
          rx: 8,
          ry: 8,
        },
        label: {
          fontSize: 12,
          fill: '#333',
          textAnchor: 'middle',
          textVerticalAnchor: 'middle',
        },
        // 新增描述文本样式
        desc: {
          fontSize: 10,
          fill: '#888',
          textAnchor: 'start',
          textVerticalAnchor: 'top',
          x: 12,
          y: 18,
        },
      },
      ports: {
        groups: {
          in: {
            position: 'left',
            attrs: {
              circle: {
                r: 4,
                magnet: true,
                stroke: '#5F95FF',
                strokeWidth: 1,
                fill: '#fff',
              },
            },
          },
          out: {
            position: 'right',
            attrs: {
              circle: {
                r: 4,
                magnet: true,
                stroke: '#5F95FF',
                strokeWidth: 1,
                fill: '#fff',
              },
            },
          },
        },
      },
    }
  }

  // 判断是否折叠
  isCollapsed() {
    return this.collapsed
  }

  // 切换折叠状态
  toggleCollapse(collapsed) {
    const target = collapsed == null ? !this.collapsed : collapsed
    
    if (target) {
      // 折叠状态
      this.expandSize = this.getSize()
      this.resize(120, 60)
    } else {
      // 展开状态
      if (this.expandSize) {
        this.resize(this.expandSize.width, this.expandSize.height)
      }
    }
    
    this.collapsed = target
  }

  // 获取子节点
  getChildren() {
    return this.getDescendants().filter(cell => cell.isNode())
  }

  // 添加子节点
  addChild(child) {
    if (child.isNode()) {
      this.addChild(child)
    }
  }

  // 移除子节点
  removeChild(child) {
    if (child.isNode()) {
      this.removeChild(child)
    }
  }
}

// 注册Group节点
Group.config() 